﻿//统一文件为utf-8编码编译
#pragma execution_character_set("utf-8")
///////////////////////////////////////////////////////////////////////////
// 文件名:     startlogdatabase.cpp
// 描述:
// 作者:      颜磊
// 创建时间:   2024-06-08 23:44
///////////////////////////////////////////////////////////////////////////
#include "startlogdatabase.h"

#include <QCoreApplication>
#include <QDateTime>
#include <QSqlQuery>
#include <QSqlError>
#include <QDebug>
#include <QDir>
#include <QUuid>

static QString guid(){
    QUuid id = QUuid::createUuid();
    QString strGuid = id.toString();
    strGuid = id.toString(QUuid::Id128);
    return strGuid;
}

static QString createMulitipleFolders(QString path){
    QDir dir(path);
    if(dir.exists(path)){
        return path;
    }
    QString parentDir = createMulitipleFolders(path.mid(0,path.lastIndexOf('/')));
    QString dirName = path.mid(path.lastIndexOf('/') + 1);
    QDir parentPath(parentDir);
    if(!dirName.isEmpty()){
        parentPath.mkdir(dirName);
    }
    return parentDir + "/" + dirName;
}
StartLogDataBase::StartLogDataBase()
{
    QString dbDir = QDir::homePath()+"/AppData/DaemonSystem";
    dbDir = "C:/Program Files/DaemonSystem/DataBase";
    createMulitipleFolders(dbDir);
    //dbPath = QCoreApplication::applicationDirPath()+"/ProgressDataBase.db";
    dbPath = dbDir+"/StartLogDataBase.db";
    dataBase = QSqlDatabase::addDatabase("QSQLITE",QString("connect_%1").arg(guid()));
    openDataBase();
}

StartLogDataBase::~StartLogDataBase()
{
    closeDataBase();
}

void StartLogDataBase::openDataBase()
{
    dataBase.setDatabaseName(dbPath);
    dataBase.open();

    QSqlQuery query(dataBase);
    if(!query.exec("select * from StartLogInfo_table")){
        if(!query.exec("create table StartLogInfo_table("
                           "id integer primary key,"
                           "start_time text,"
                           "start_type int,"
                           "progress_name text,"
                           "progress_path text,"
                           "remark text)"))
            qCritical()<< __FUNCTION__ << query.lastError();
    }
}

void StartLogDataBase::closeDataBase()
{
    if(dataBase.isOpen())
        dataBase.close();
}

int StartLogDataBase::getStartLogInfoCount(QString progressName)
{
    int res = -1;
    QSqlQuery query(dataBase);
    QString sqlStr = QString("select count(*) from StartLogInfo_table where progress_name like '%%1%'").arg(progressName);
    if(!progressName.isEmpty()){
        sqlStr.append(QString(" where progress_name like '%%1%'").arg(progressName));
    }
    if(!query.exec(sqlStr)){
        qCritical()<< __FUNCTION__ << query.lastError()<< sqlStr;
    }else{
        if(query.next()){
            res=query.value(0).toInt();
        }else{
            qCritical()<< __FUNCTION__ << query.lastError()<< sqlStr;
        }
    }
    return res;
}

int StartLogDataBase::getStartLogInfoList(QList<StartLogInfo> &list,QString progressName, int pageCount, int nowPage)
{
    int res = -1;
    QSqlQuery query(dataBase);
    QString sqlStr = QString("select * from StartLogInfo_table where progress_name like '%%1%'").arg(progressName);
    sqlStr.append(QString(" order by id desc"));
    sqlStr.append(QString(" limit %1 offset %2").arg(pageCount).arg((nowPage - 1)));
//    qDebug() << sqlStr;
    if(!query.exec(sqlStr)){
        qCritical()<< __FUNCTION__ << query.lastError()<< sqlStr;
    }else{
        while (query.next()) {
            list.append(StartLogInfo(
                            query.value(0).toInt(),
                            query.value(1).toString().toStdString(),
                            query.value(2).toInt(),
                            query.value(3).toString().toStdString(),
                            query.value(4).toString().toStdString(),
                            query.value(5).toString().toStdString()
                            ));
        }
        res = list.size();
    }
    return res;
}

void StartLogDataBase::insertStartLog(StartLogInfo info)
{
    QSqlQuery query(dataBase);
    QString sqlStr = QString("insert into StartLogInfo_table(start_time,start_type,progress_name,progress_path,remark) values('%1',%2,'%3','%4','%5')")
            .arg(QString(info.start_time))
            .arg((int)info.start_type)
            .arg(QString(info.progress_name))
            .arg(QString(info.progress_path))
            .arg(QString(info.remark));
    //qDebug() << sqlStr;
    if(!query.exec(sqlStr)){
        qCritical()<< __FUNCTION__ << query.lastError()<< sqlStr;
    }
}

void StartLogDataBase::clearAll()
{
    QSqlQuery query(dataBase);
    QString sqlStr = QString("delete from StartLogInfo_table");
    if(!query.exec(sqlStr)){
        qCritical()<< __FUNCTION__ << query.lastError()<< sqlStr;
    }
}
